package com.leijie.nio;

import java.nio.ByteBuffer;

/**
 * 普通buffer能转换成只读buffer
 * 只读Buffer不能转换成普通读写buffer
 */
public class NioTest7 {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.getClass());
        for (int i=0;i<buffer.capacity();++i){
            buffer.put((byte) i);
        }
        //如果希望调用端只能读 不能写可以使用只读buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //put 会直接抛出异常throw new ReadOnlyBufferException();
        readOnlyBuffer.put((byte) 1);
    }

}
